package compendiumAid.presentation.selectedElements
{
	import compendiumAid.application.AddElementsToSelectionEvent;
	import compendiumAid.application.AlwaysViewElementEvent;
	import compendiumAid.application.ViewElementEvent;
	import compendiumAid.domain.ElementUtil;
	import compendiumAid.domain.vo.ElementVO;
	import compendiumAid.presentation.PresentationModel;
	import compendiumAid.presentation.sortMethods.SortMethodsPM;
	
	import mx.collections.ArrayCollection;
	import mx.collections.Sort;
	import mx.collections.SortField;
	import mx.events.CollectionEvent;
	
	[ManagedEvents(names="ViewElementEvent")]
	public class SelectedElementsPM extends PresentationModel
	{
		[Bindable]
		public var elements:ArrayCollection = new ArrayCollection;
		
		[Inject]
		public var sortMethodsPM:SortMethodsPM;
		
		private var alwaysView:Boolean;
		private var _autoSort:Boolean;
		
		[Init]
		public function init():void
		{
			alwaysView = false;
			autoSort = true;
		}
		
		[MessageHandler]
		public function addElements(event:AddElementsToSelectionEvent):void
		{
			for each (var elementVO:ElementVO in event.elements)
			{
				if (alreadySelected(elementVO) == false)
				{
					elements.addItem(elementVO);
				}
			}
		}
		
		public function removeElements(elementsVector:Vector.<Object>):void
		{
			for each (var elementVO:ElementVO in elementsVector)
			{
				var index:int = elements.getItemIndex(elementVO);
				
				if ((index >= 0) && (index < elements.length))
				{
					elements.removeItemAt(index);
				}
			}
		}
		
		public function removeAllElements():void
		{
			elements.removeAll();
		}
		
		[Bindable]
		public function set autoSort(value:Boolean):void
		{
			_autoSort = value;
			
			setSortForElements(_autoSort);
			
			if (value == true)
			{
				sortMethodsPM.selected.addEventListener(CollectionEvent.COLLECTION_CHANGE, sortElementsListener);
			}
			else
			{
				sortMethodsPM.selected.removeEventListener(CollectionEvent.COLLECTION_CHANGE, sortElementsListener);
			}
		}
		
		public function get autoSort():Boolean
		{
			return _autoSort;
		}
		
		public function selectSortCriteria():void
		{
			sortMethodsPM.showPopUp();
		}
		
		public function viewElement(elementObject:Object):void
		{
			var elementVO:ElementVO = ElementUtil.elementObjectToElementVO(elementObject);
			dispatcher(new ViewElementEvent(elementVO));
		}
		
		public function selectElement(elementObject:Object):void
		{
			if (alwaysView == true)
			{
				viewElement(elementObject);
			}
		}
		
		[MessageHandler]
		public function alwaysViewElement(event:AlwaysViewElementEvent):void
		{
			alwaysView = event.alwaysView;
		}
		
		
		private function sortElementsListener(event:CollectionEvent):void
		{
			setSortForElements(true);
		}
		
		private function alreadySelected(elementVO:ElementVO):Boolean
		{
			for each (var testElementVO:ElementVO in elements)
			{
				if (elementVO.id == testElementVO.id)
				{
					return true;
				}
			}
			
			return false;
		}
		
		private function setSortForElements(sort:Boolean):void
		{
			var dataSortField:SortField;
			var dataSort:Sort = new Sort();
			
			if (sort == true)
			{
				var dataSortFields:Array = new Array();
				
				for each (var field:String in sortMethodsPM.selected)
				{
					dataSortField = new SortField(field.toLowerCase());
					dataSortFields.push(dataSortField);
				}
				
				dataSort.fields = dataSortFields;
			}
			
			elements.sort = dataSort;
			elements.refresh();
		}
	}
}